import Message from 'element-ui/lib/message';
import MessageBox from 'element-ui/lib/message-box';

// 默认配置
const msgOptions = { confirmButtonText: '确认', cancelButtonText: '取消', showCancelButton: true, center: true, type: 'warning' };

/**
 *  确认弹窗装饰器
 * @param {*} title 确认弹窗标题文字
 * @param {*} content 确认弹窗内容文字
 * @param {*} option 确认弹窗参数
 * @returns
 */
export default function(title, content, option = {}) {
  return function(target, propertyKey, descriptor) {
    const method = descriptor.value;
    descriptor.value = function(...args) {
      const that = this._isVue ? this : target;
      const options = Object.assign(msgOptions, option);

      // 弹窗确认框
      MessageBox.confirm(content, title, options)
        .then(() => method.apply(that, args))
        .catch(err => (err === 'cancel' ? Message.warning('已取消操作！') : console.log(err)));
    };
  };
}
